<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LANGUAGE Safe #-}</span><span>
</span><span id="line-2"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-3"></span><span class="hs-comment">-- |</span><span>
</span><span id="line-4"></span><span class="hs-comment">-- Module      :  Control.Monad.IO.Class</span><span>
</span><span id="line-5"></span><span class="hs-comment">-- Copyright   :  (c) Andy Gill 2001,</span><span>
</span><span id="line-6"></span><span class="hs-comment">--                (c) Oregon Graduate Institute of Science and Technology, 2001</span><span>
</span><span id="line-7"></span><span class="hs-comment">-- License     :  BSD-style (see the file LICENSE)</span><span>
</span><span id="line-8"></span><span class="hs-comment">--</span><span>
</span><span id="line-9"></span><span class="hs-comment">-- Maintainer  :  R.Paterson@city.ac.uk</span><span>
</span><span id="line-10"></span><span class="hs-comment">-- Stability   :  experimental</span><span>
</span><span id="line-11"></span><span class="hs-comment">-- Portability :  portable</span><span>
</span><span id="line-12"></span><span class="hs-comment">--</span><span>
</span><span id="line-13"></span><span class="hs-comment">-- Class of monads based on @IO@.</span><span>
</span><span id="line-14"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-15"></span><span>
</span><span id="line-16"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">Control.Monad.IO.Class</span><span> </span><span class="hs-special">(</span><span>
</span><span id="line-17"></span><span>    </span><span class="annot"><a href="Control.Monad.IO.Class.html#MonadIO"><span class="hs-identifier">MonadIO</span></a></span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span>
</span><span id="line-18"></span><span>  </span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-19"></span><span>
</span><span id="line-20"></span><span class="hs-comment">-- | Monads in which 'IO' computations may be embedded.</span><span>
</span><span id="line-21"></span><span class="hs-comment">-- Any monad built by applying a sequence of monad transformers to the</span><span>
</span><span id="line-22"></span><span class="hs-comment">-- 'IO' monad will be an instance of this class.</span><span>
</span><span id="line-23"></span><span class="hs-comment">--</span><span>
</span><span id="line-24"></span><span class="hs-comment">-- Instances should satisfy the following laws, which state that 'liftIO'</span><span>
</span><span id="line-25"></span><span class="hs-comment">-- is a transformer of monads:</span><span>
</span><span id="line-26"></span><span class="hs-comment">--</span><span>
</span><span id="line-27"></span><span class="hs-comment">-- * @'liftIO' . 'return' = 'return'@</span><span>
</span><span id="line-28"></span><span class="hs-comment">--</span><span>
</span><span id="line-29"></span><span class="hs-comment">-- * @'liftIO' (m &gt;&gt;= f) = 'liftIO' m &gt;&gt;= ('liftIO' . f)@</span><span>
</span><span id="line-30"></span><span>
</span><span id="line-31"></span><span class="hs-keyword">class</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Base.html#Monad"><span class="hs-identifier hs-type">Monad</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679566093"><span class="hs-identifier hs-type">m</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span id="MonadIO"><span class="annot"><a href="Control.Monad.IO.Class.html#MonadIO"><span class="hs-identifier hs-var">MonadIO</span></a></span></span><span> </span><span id="local-6989586621679566093"><span class="annot"><a href="#local-6989586621679566093"><span class="hs-identifier hs-type">m</span></a></span></span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-32"></span><span>    </span><span class="hs-comment">-- | Lift a computation from the 'IO' monad.</span><span>
</span><span id="line-33"></span><span>    </span><span class="hs-comment">-- This allows us to run IO computations in any monadic stack, so long as it supports these kinds of operations</span><span>
</span><span id="line-34"></span><span>    </span><span class="hs-comment">-- (i.e. 'IO' is the base monad for the stack).</span><span>
</span><span id="line-35"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-36"></span><span>    </span><span class="hs-comment">-- === __Example__</span><span>
</span><span id="line-37"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-38"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-39"></span><span>    </span><span class="hs-comment">-- &gt; import Control.Monad.Trans.State -- from the &quot;transformers&quot; library</span><span>
</span><span id="line-40"></span><span>    </span><span class="hs-comment">-- &gt;</span><span>
</span><span id="line-41"></span><span>    </span><span class="hs-comment">-- &gt; printState :: Show s =&gt; StateT s IO ()</span><span>
</span><span id="line-42"></span><span>    </span><span class="hs-comment">-- &gt; printState = do</span><span>
</span><span id="line-43"></span><span>    </span><span class="hs-comment">-- &gt;   state &lt;- get</span><span>
</span><span id="line-44"></span><span>    </span><span class="hs-comment">-- &gt;   liftIO $ print state</span><span>
</span><span id="line-45"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-46"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-47"></span><span>    </span><span class="hs-comment">-- Had we omitted @'liftIO'@, we would have ended up with this error:</span><span>
</span><span id="line-48"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-49"></span><span>    </span><span class="hs-comment">-- &gt; &#8226; Couldn't match type &#8216;IO&#8217; with &#8216;StateT s IO&#8217;</span><span>
</span><span id="line-50"></span><span>    </span><span class="hs-comment">-- &gt;  Expected type: StateT s IO ()</span><span>
</span><span id="line-51"></span><span>    </span><span class="hs-comment">-- &gt;    Actual type: IO ()</span><span>
</span><span id="line-52"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-53"></span><span>    </span><span class="hs-comment">-- The important part here is the mismatch between @StateT s IO ()@ and @'IO' ()@.</span><span>
</span><span id="line-54"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-55"></span><span>    </span><span class="hs-comment">-- Luckily, we know of a function that takes an @'IO' a@ and returns an @(m a)@: @'liftIO'@,</span><span>
</span><span id="line-56"></span><span>    </span><span class="hs-comment">-- enabling us to run the program and see the expected results:</span><span>
</span><span id="line-57"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-58"></span><span>    </span><span class="hs-comment">-- @</span><span>
</span><span id="line-59"></span><span>    </span><span class="hs-comment">-- &gt; evalStateT printState &quot;hello&quot;</span><span>
</span><span id="line-60"></span><span>    </span><span class="hs-comment">-- &quot;hello&quot;</span><span>
</span><span id="line-61"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-62"></span><span>    </span><span class="hs-comment">-- &gt; evalStateT printState 3</span><span>
</span><span id="line-63"></span><span>    </span><span class="hs-comment">-- 3</span><span>
</span><span id="line-64"></span><span>    </span><span class="hs-comment">-- @</span><span>
</span><span id="line-65"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-66"></span><span>    </span><span id="local-6989586621679566091"><span id="liftIO"><span class="annot"><a href="Control.Monad.IO.Class.html#liftIO"><span class="hs-identifier hs-type">liftIO</span></a></span></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679566091"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679566093"><span class="hs-identifier hs-type">m</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679566091"><span class="hs-identifier hs-type">a</span></a></span></span><span>
</span><span id="line-67"></span><span>
</span><span id="line-68"></span><span class="hs-comment">-- | @since 4.9.0.0</span><span>
</span><span id="line-69"></span><span class="hs-keyword">instance</span><span> </span><span class="annot"><a href="Control.Monad.IO.Class.html#MonadIO"><span class="hs-identifier hs-type">MonadIO</span></a></span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-70"></span><span>    </span><span id="local-6989586621679566080"><span class="annot"><span class="annottext">liftIO :: forall a. IO a -&gt; IO a
</span><a href="#local-6989586621679566080"><span class="hs-identifier hs-var hs-var hs-var hs-var">liftIO</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">IO a -&gt; IO a
forall a. a -&gt; a
</span><a href="GHC.Base.html#id"><span class="hs-identifier hs-var">id</span></a></span><span>
</span><span id="line-71"></span><span>
</span><span id="line-72"></span></pre></body></html>